home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ für Kids
/
C++ for kids.iso
/
SETUP
/
US
/
CBUILDER
/
DATA.Z
/
BITSET.H
< prev
next >
Wrap
C/C++ Source or Header
|
1997-02-13
|
18KB
|
629 lines
#ifndef __STD_BITS
#define __STD_BITS
/* $Revision: 8.1 $ */
/***************************************************************************
*
* bitset - class bitset declaration
*
* $Id: bitset,v 1.55 1995/09/29 23:52:59 smithey Exp $
*
***************************************************************************
*
* (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
* ALL RIGHTS RESERVED
*
* The software and information contained herein are proprietary to, and
* comprise valuable trade secrets of, Rogue Wave Software, Inc., which
* intends to preserve as trade secrets such software and information.
* This software is furnished pursuant to a written license agreement and
* may be used, copied, transmitted, and stored only in accordance with
* the terms of such license and with the inclusion of the above copyright
* notice. This software and information or any other copies thereof may
* not be provided or otherwise made available to any other person.
*
* Notwithstanding any other lease or license that may pertain to, or
* accompany the delivery of, this computer software and information, the
* rights of the Government regarding its use, reproduction and disclosure
* are as set forth in Section 52.227-19 of the FARS Computer
* Software-Restricted Rights clause.
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7013.
* Contractor/Manufacturer is Rogue Wave Software, Inc.,
* P.O. Box 2328, Corvallis, Oregon 97339.
*
* This computer software and information is distributed with "restricted
* rights." Use, duplication or disclosure is subject to restrictions as
* set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
* Computer Software-Restricted Rights (April 1985)." If the Clause at
* 18-52.227-74 "Rights in Data General" is specified in the contract,
* then the "Alternate III" clause applies.
*
**************************************************************************/
#include <stdcomp.h>
#include <stddefs.h>
#ifndef RWSTD_NO_NEW_HEADER
#include <climits>
#include <cstddef>
#else
#include <limits.h>
#include <stddef.h>
#endif
#ifdef RW_STD_IOSTREAM
#include <iosfwd>
#else
class ostream;
class istream;
#endif
#ifndef RWSTD_NO_EXCEPTIONS
#ifdef RW_STD_EXCEPT
#include <stdexcept>
#endif
#endif
#include <string>
#ifndef RWSTD_NO_NAMESPACE
namespace std {
#endif
//
// Exception error messages.
//
extern char RWSTDExport __rw_bitset_InvalidPosition[];
extern char RWSTDExport __rw_bitset_InvalidCtorArgument[];
extern char RWSTDExport __rw_bitset_ConversionOverflow[];
template <size_t N>
class bitset
{
private:
//
// The type of array in which we store the bits.
//
typedef unsigned int VectorType;
//
// Number of bits in an array element.
//
enum { BitsPerChunk = CHAR_BIT*sizeof(unsigned int) };
//
// Number of array elements.
//
#ifndef RWSTD_BC5_ENUM_BUG
enum { NumOfElems = N == 0 ? 1 : 1 + ((N - 1) / BitsPerChunk) };
#define NELEMENTS NumOfElems
#else
int NumOfElems () const
{
return N == 0 ? 1 : 1 + ((N - 1) / BitsPerChunk);
}
#define NELEMENTS NumOfElems()
#endif /*RWSTD_BC5_ENUM_BUG*/
//
// Number of bits in an unsigned long.
//
enum { BitsInUnsignedLong = CHAR_BIT*sizeof(unsigned long) };
//
// The array of bits.
//
#ifndef RWSTD_BC5_ENUM_BUG
VectorType bits[NELEMENTS];
#else
VectorType* bits;
#endif /*RWSTD_BC5_ENUM_BUG*/
protected:
//
// Is pos a valid bitset position?
//
bool valid_position (size_t pos) const RWSTD_THROW_SPEC_NULL
{
return N > pos ? true : false;
}
//
// Given a bit position `pos', returns the index into the appropriate
// chunk in bits[] such that 0 <= index < BitsPerChunk.
//
size_t index (size_t pos) const RWSTD_THROW_SPEC_NULL
{
#if UINT_MAX == 256
return 7 & pos;
#elif UINT_MAX == 65535
return 15 & pos;
#elif UINT_MAX == 4294967295
return 31 & pos;
#elif UINT_MAX == 18446744073709551616
return 63 & pos;
#else
return pos % BitsPerChunk;
#endif
}
public:
//
// bit reference
//
class reference
{
friend class bitset<N>;
private:
bitset<N>& ref;
size_t pos;
reference (bitset<N>& r, size_t p) RWSTD_THROW_SPEC_NULL
: ref(r), pos(p) {}
public:
~reference() RWSTD_THROW_SPEC_NULL {}
//
// for b[i] = x;
//
reference& operator= (bool val) RWSTD_THROW_SPEC_NULL
{
ref.set(pos, val); return *this;
}
//
// for b[i] = b[j];
//
reference& operator= (const reference& rhs) RWSTD_THROW_SPEC_NULL
{
ref.set(pos, rhs.ref.test(rhs.pos)); return *this;
}
//
// for x = ~b[i];
//
bool operator~ () const RWSTD_THROW_SPEC_NULL { return !ref.test(pos);}
//
// for x = b[i];
//
operator bool () const RWSTD_THROW_SPEC_NULL { return ref.test(pos); }
//
// flips the bit
//
reference& flip() RWSTD_THROW_SPEC_NULL { ref.flip(pos); return *this;}
};
//
// constructors
//
bitset () RWSTD_THROW_SPEC_NULL
{
#ifndef RWSTD_BC5_ENUM_BUG
memset(bits, 0, sizeof(bits));
#else
bits = new VectorType[NELEMENTS];
//
// TODO -- check for bits == 0 here?
//
memset(bits, 0, NELEMENTS*sizeof(VectorType));
#endif /*RWSTD_BC5_ENUM_BUG*/
}
bitset (unsigned long val) RWSTD_THROW_SPEC_NULL
{
//
// Initialize first M bit positions to the corresponding
// bit values in val. M is the smaller of N and the value
// CHAR_BIT * sizeof(unsigned long).
//
#ifndef RWSTD_BC5_ENUM_BUG
memset(bits, 0, sizeof(bits));
#else
bits = new VectorType[NELEMENTS];
//
// TODO -- check for bits == 0 here?
//
memset(bits, 0, NELEMENTS*sizeof(VectorType));
#endif /*RWSTD_BC5_ENUM_BUG*/
size_t M = N < BitsInUnsignedLong ? N : BitsInUnsignedLong;
for (size_t i = 0; i < M; i++)
if (val & (1 << i))
set(i);
}
explicit bitset (const string& str,
size_t pos = 0,
size_t n = (size_t) -1)
RWSTD_THROW_SPEC((out_of_range, invalid_argument));
//
// We explicitly defined the copy constructor, though
// WP 17.2.2.2 allows us to use the default generated one.
//
bitset (const bitset<N>& rhs) RWSTD_THROW_SPEC_NULL
{
#ifndef RWSTD_BC5_ENUM_BUG
memcpy(bits, rhs.bits, sizeof(bits));
#else
bits = new VectorType[NELEMENTS];
//
// TODO -- check for bits == 0 here?
//
memcpy(bits, rhs.bits, NELEMENTS*sizeof(VectorType));
#endif /*RWSTD_BC5_ENUM_BUG*/
}
//
// We explicitly defined the assignment, though
// WP 17.2.2.2 allows us to use the default generated one.
//
bitset<N>& operator= (const bitset<N>& rhs) RWSTD_THROW_SPEC_NULL
{
if (!(this == &rhs))
#ifndef RWSTD_BC5_ENUM_BUG
memcpy(bits, rhs.bits, sizeof(bits));
#else
memcpy(bits, rhs.bits, NELEMENTS*sizeof(VectorType));
#endif /*RWSTD_BC5_ENUM_BUG*/
return *this;
}
#ifdef RWSTD_BC5_ENUM_BUG
~bitset () RWSTD_THROW_SPEC_NULL { delete [] bits; }
#endif
//
// bitset operations
//
bitset<N>& operator&= (const bitset<N>& rhs) RWSTD_THROW_SPEC_NULL
{
for (size_t i = 0; i < NELEMENTS; i++)
bits[i] &= rhs.bits[i];
return *this;
}
bitset<N>& operator|= (const bitset<N>& rhs) RWSTD_THROW_SPEC_NULL
{